﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Utils;
using Shpm.System;
using Shpm.App.Master.Bean;
using Shpm.System.Validator;
using Shpm.System.Web.Control;

public partial class App_Input_InterviewEvaluateUpdForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            //控件使用权限控制
            CommonDictionaryDao commonDictionaryDao = new CommonDictionaryDao();
            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            paramBean.Catalog = "T_INTERVIEW.INTERVIEW_STATUS";
            IList dataList = commonDictionaryDao.FindByCatalog(paramBean);
            this.radStatus.DataSource = dataList;
            this.radStatus.DataBind();

           // Context.Items["INTERVIEW_ID"] = 3;
            //画面初始化
            if(Context.Items["INTERVIEW_ID"] != null)
            {
                ViewState["INTERVIEW_ID"] = Context.Items["INTERVIEW_ID"];
                InitPage(Convert.ToInt32(Context.Items["INTERVIEW_ID"]));
            }
        }
    }

    private void InitPage(int interviewId)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;

       


        //检索
        EmployInterviewDao dao = new EmployInterviewDao();
        EmployInterviewBean bean = new EmployInterviewBean();
        bean.InterviewId = interviewId;
        bean = dao.GetInterInfo(bean);

        //初始化
        this.labName.Text = bean.InterviewName;
        this.labPosition.Text = bean.JobName;
        
        //用人部门评估人
        EmployeeDao employeeDao = new EmployeeDao();
        EmployeeBean employeeBean = new EmployeeBean();
        if (!string.IsNullOrEmpty(bean.EvaluaterId))
        {
            
            ViewState["EVALUATER_ID"] = bean.EvaluaterId;
            employeeBean.EmployeeId = bean.EvaluaterId;
        }
        else 
        {
            ViewState["EVALUATER_ID"] = emplyId;
            employeeBean.EmployeeId = emplyId;
        }
        employeeBean = employeeDao.GetEmployee(employeeBean);
        this.labEvaName.Text = employeeBean.EmployeeName;



        this.ChkAppearance.Items[0].Selected = bean.InterviewVocationally.Value == 1;
        this.ChkAppearance.Items[1].Selected = bean.InterviewSimpilicity.Value == 1;
        this.ChkAppearance.Items[2].Selected = bean.InterviewCvility.Value == 1;
        this.ChkAppearance.Items[3].Selected = bean.InterviewBlood.Value == 1;

        //
        if (!string.IsNullOrEmpty(bean.EvaluaterId))
        {
            this.radComposedness.SelectedValue = bean.InterviewComposedness.Value.ToString();
            this.radExperssion.SelectedValue = bean.InterviewExperssion.Value.ToString();
            this.radLogic.SelectedValue = bean.InterviewLogic.Value.ToString();
            this.radResponse.SelectedValue = bean.InterviewResponse.Value.ToString();
        }
     
        //this.ChkCommunicate.Items[4].Selected = bean.InterviewComposedness.Value == 0;
        //this.ChkCommunicate.Items[5].Selected = bean.InterviewExperssion.Value == 0;
        //this.ChkCommunicate.Items[6].Selected = bean.InterviewLogic.Value == 0;
        //this.ChkCommunicate.Items[7].Selected = bean.InterviewResponse.Value == 0;

       

        this.txtIntersts.Text = bean.InterviewInterest;
        this.txtKnowledge.Text = bean.InterviewKnowledge;
        this.txtLongevity.Text = bean.InterviewLongevity;
        this.txtTrait.Text = bean.InterviewTrait;
        this.txtExperience.Text = bean.InterviewExperience;
        this.txtAppraise.Text = bean.InterviewAppraise;
        this.txtWriting.Text = bean.InterviewWriting;
        this.txtContent.Text = bean.InterviewContent;
        this.txtExamanation.Text = bean.InterviewExamanation;
        this.txtExpectation.Text = bean.InterviewExpectation;
        this.txtSelectvity.Text = bean.InterviewSelectvity;
        this.txtRepeness.Text = bean.InterviewRepeness;
        this.txtAim.Text = bean.InterviewAim;
        this.txtRaise.Text = bean.InterviewRaise;
        this.txtPotential.Text = bean.InterviewPotential;
        this.txtFeasibility.Text = bean.InterviewFeasibility;
        this.txtEmployMent.Text = bean.InterviewEmployment;
        this.txtPersonnal.Text = bean.InterviewPersonnel;
        this.txtDimision.Text = bean.InterviewDimission;
        this.txtBespokeDate.Text =bean.InterviewBespokeDate == null?"": DateUtils.convertToString(bean.InterviewBespokeDate.Value);
        this.txtEmployDate.Text = bean.InterviewEmployDate == null ? "": DateUtils.convertToString(bean.InterviewEmployDate.Value);
        this.radStatus.SelectedValue = (this.radStatus.Items.FindByValue(bean.InterviewStatus) == null) ? null : bean.InterviewStatus;


        PersonEmployeeBean employbean = new PersonEmployeeBean();
        EmployApplyDao employApplyDao = new EmployApplyDao();

        employbean.EmployeeId = emplyId;
        employbean = employApplyDao.CheckAuthority(employbean);
        if (employbean != null)
        {
            //“保存“按钮 =〉用人部门，HR使用
            //“提交“按钮 =〉HR使用
            //“审核“按钮 =〉HR总监使用
            if (Convert.ToInt32(employbean.JobLimit) >= 3 || bean.CommitFlg.Value == 1)
            {
                if (!bean.DepartId.Equals(employbean.DepartId))
                {
                    this.txtAim.Enabled = false;
                    this.txtRaise.Enabled = false;
                    this.txtPotential.Enabled = false;
                    this.txtFeasibility.Enabled = false;
                    this.txtEmployMent.Enabled = false;
                    this.txtPersonnal.Enabled = false;
                    this.txtDimision.Enabled = false;
                    this.txtBespokeDate.Enabled = false;
                   
                   
                }
                this.radStatus.Enabled = false;
                this.txtEmployDate.Enabled = false;

                if (bean.AuditingFlg.Value == 0)
                {
                    this.btn_Auditing.Enabled = bean.CommitFlg.Value == 1;
                    this.btn_Submit.Enabled = bean.CommitFlg.Value == 0;
                    if (btn_Submit.Enabled)
                    {
                        this.ChkAppearance.Enabled = true;
                        this.radComposedness.Enabled = true;
                        this.radExperssion.Enabled = true;
                        this.radLogic.Enabled = true;
                        this.radResponse.Enabled = true;
                        this.radStatus.Enabled = false;
                    }
                    else
                    {
                        //设置审核时控件状态
                        SetAuditing();
                        this.txtEmployDate.Enabled = true;
                        this.radStatus.Enabled = true;
                    }



                }
                else 
                {
                    //设置审核时控件状态
                    SetAuditing();
                    this.btn_Auditing.Enabled = false;
                }
            }
            else if (employbean.DepartId == "2")
            {
                if (!bean.DepartId.Equals(employbean.DepartId))
                {
                    this.txtAim.Enabled = false;
                    this.txtRaise.Enabled = false;
                    this.txtPotential.Enabled = false;
                    this.txtFeasibility.Enabled = false;
                    this.txtEmployMent.Enabled = false;
                    this.txtPersonnal.Enabled = false;
                    this.txtDimision.Enabled = false;
                    this.txtBespokeDate.Enabled = false;
                    this.txtEmployDate.Enabled = false;
                    this.radStatus.Enabled = false;

                }
               
                this.btn_Save.Enabled = bean.CommitFlg.Value == 0;
                this.btn_Submit.Enabled = bean.CommitFlg.Value == 0;
                this.btn_Auditing.Enabled = false;
            }
            else
            {
                this.ChkAppearance.Enabled = false;
                this.radComposedness.Enabled = false;
                this.radExperssion.Enabled = false;
                this.radLogic.Enabled = false;
                this.radResponse.Enabled = false;
                this.txtIntersts.Enabled = false;
                this.txtExpectation.Enabled = false;
                this.txtSelectvity.Enabled = false;
                this.txtEmployDate.Enabled = false;
                this.radStatus.Enabled = false;

                this.btn_Auditing.Enabled = false;
                this.btn_Submit.Enabled = false;
                if (bean.DepartId.Equals(employbean.DepartId))
                {
                    this.btn_Save.Enabled = bean.CommitFlg.Value == 0;
                    

                }
                else 
                {
                    SetAuditing();
                    this.btn_Save.Enabled = false;
                 
                }
               
            }
        }

        
    }

    private void SetAuditing()
    {
        this.ChkAppearance.Enabled = false;
        this.radComposedness.Enabled = false;
        this.radExperssion.Enabled = false;
        this.radLogic.Enabled = false;
        this.radResponse.Enabled = false;
        this.txtIntersts.Enabled = false;

        this.txtKnowledge.Enabled = false;
        this.txtLongevity.Enabled = false;
        this.txtTrait.Enabled = false;
        this.txtExperience.Enabled = false;
        this.txtAppraise.Enabled = false;
        this.txtWriting.Enabled = false;
        this.txtContent.Enabled = false;
        this.txtExamanation.Enabled = false;
        this.txtExpectation.Enabled = false;
        this.txtSelectvity.Enabled = false;
        this.txtRepeness.Enabled = false;
        this.txtAim.Enabled = false;
        this.txtRaise.Enabled = false;
        this.txtPotential.Enabled = false;
        this.txtFeasibility.Enabled = false;
        this.txtEmployMent.Enabled = false;
        this.txtPersonnal.Enabled = false;
        this.txtDimision.Enabled = false;
        this.txtBespokeDate.Enabled = false;



        this.txtExpectation.Enabled = false;
        this.txtSelectvity.Enabled = false;
        this.txtEmployDate.Enabled = false;
        this.radStatus.Enabled = false;

        this.btn_Save.Enabled = false;
        this.btn_Submit.Enabled = false;
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;


        EmployInterviewDao dao = new EmployInterviewDao();
        EmployInterviewBean bean = new EmployInterviewBean();
       

        PersonEmployeeBean employbean = new PersonEmployeeBean();
        EmployApplyDao employApplyDao = new EmployApplyDao();

        employbean.EmployeeId = emplyId;
        employbean = employApplyDao.CheckAuthority(employbean);
         if (employbean != null)
        {

            if (employbean.DepartId == "2")
            {
                if (this.ComValidator1.DoValidator() == false)
                {

                    ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

                    return;
                }
            }
            bean.InterviewId = Convert.ToInt32(ViewState["INTERVIEW_ID"]);
            bean.InterviewVocationally = this.ChkAppearance.Items[0].Selected? (byte)1:(byte)0;
            bean.InterviewSimpilicity = this.ChkAppearance.Items[1].Selected ? (byte)1 : (byte)0;
            bean.InterviewCvility = this.ChkAppearance.Items[2].Selected ? (byte)1 : (byte)0;
            bean.InterviewBlood = this.ChkAppearance.Items[3].Selected ? (byte)1 : (byte)0;

            bean.InterviewComposedness = Convert.ToByte(this.radComposedness.SelectedValue);
            bean.InterviewExperssion = Convert.ToByte(this.radExperssion.SelectedValue);
            bean.InterviewLogic = Convert.ToByte(this.radLogic.SelectedValue);
            bean.InterviewResponse = Convert.ToByte(this.radResponse.SelectedValue);




            bean.InterviewInterest = this.txtIntersts.Text;
            bean.InterviewKnowledge = this.txtKnowledge.Text;
            bean.InterviewLongevity = this.txtLongevity.Text;
            bean.InterviewTrait = this.txtTrait.Text;
            bean.InterviewExperience = this.txtExperience.Text;
            bean.InterviewAppraise = this.txtAppraise.Text;
            bean.InterviewWriting = this.txtWriting.Text;
            bean.InterviewContent = this.txtContent.Text;
            bean.InterviewExamanation = this.txtExamanation.Text;
            bean.InterviewExpectation = this.txtExpectation.Text;
            bean.InterviewSelectvity = this.txtSelectvity.Text;
            bean.InterviewRepeness = this.txtRepeness.Text;
            bean.InterviewAim = this.txtAim.Text;
            bean.InterviewRaise = this.txtRaise.Text;
            bean.InterviewPotential = this.txtPotential.Text;
            bean.InterviewFeasibility = this.txtFeasibility.Text;
            bean.InterviewEmployment = this.txtEmployMent.Text;
            bean.InterviewPersonnel = this.txtPersonnal.Text;
            bean.InterviewDimission = this.txtDimision.Text;
            if (!string.IsNullOrEmpty(this.txtBespokeDate.Text))
            bean.InterviewBespokeDate = DateUtils.convertToDate(this.txtBespokeDate.Text);
            bean.CommitFlg = "COMPLETE".Equals((sender as Button).CommandName) ? Convert.ToByte(1) : Convert.ToByte(0);
            
            bean.AuditingFlg = 0;
            bean.EvaluaterId = Convert.ToString(ViewState["EVALUATER_ID"]);
            dao.UpdInterview(bean);
            ((Design_MasterPage)this.Master).InfoMessage =
                     HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            if ("COMPLETE".Equals((sender as Button).CommandName))
            {
                EmployeeDao employeeDao = new EmployeeDao();
                EmployeeBean employeeBean = new EmployeeBean();
                employeeBean.DepartId = "2";
                employeeBean.JobLimit = "3";
                IList list = employeeDao.FindEmployees(employeeBean);
                if(list.Count>0)
                {
                    MessageBean beanMsg = new MessageBean();
                    beanMsg.Subject = "面试申请--" + DateUtils.convertToString(DateTime.Now);
                    beanMsg.Message = DateUtils.convertToString(DateTime.Now) + ":面试申请已经确认，烦请领导批准！";
                    beanMsg.MessageType = 3;
                    beanMsg.SendDate = DateTime.Now;
                    beanMsg.ReplyFlag = false;
                    beanMsg.ReaderHistoryFlag = false;
                    beanMsg.ImportantFlag = false;
                    beanMsg.EmployeeId = emplyId;
                    beanMsg.UserId = loginId;


                    CommonSendMessageUtils sendMsg = null;
                    for (int i = 0; i < list.Count;i++ )
                    {
                        employeeBean = (list[i] as EmployeeBean);
                        sendMsg = new CommonSendMessageUtils(beanMsg, employeeBean.EmployeeId);
                        sendMsg.SendMessage();   
                    }
                   
                }
            }

            InitPage(Convert.ToInt32(ViewState["INTERVIEW_ID"]));

        }

    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/EmployInterviewSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);
    }
    protected void btn_Auditing_Click(object sender, EventArgs e)
    {
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        MstSysUserBean mstSysUserBean =
            sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
        MstEmployeeBean mstEmpBean =
            sysUserBean == null ? null : sysUserBean.getEmployeeBean();
        string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
        string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;


        ComValidateChecker validator = new ComValidateChecker();
        //validator.Add(new CheckRequired(txtEmployDate.Text, new ComMessage("E0082", "录用到岗日期")));
        validator.Add(new CheckDate(txtEmployDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "录用到岗日期")));
        //没通过
        if (!validator.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
            return;
        }


        EmployInterviewDao dao = new EmployInterviewDao();
        EmployInterviewBean bean = new EmployInterviewBean();
       
        bean.InterviewStatus = this.radStatus.SelectedValue;
        bean.AuditingFlg = 1;
        bean.InterviewId = Convert.ToInt32(ViewState["INTERVIEW_ID"]);
        //被录用
        if ("03".Equals(this.radStatus.SelectedValue) && !string.IsNullOrEmpty(txtEmployDate.Text))
        {
            bean.InterviewEmployDate = DateUtils.convertToDate(this.txtEmployDate.Text);
        }
        this.btn_Auditing.Enabled = false;
        dao.UpdAuditing(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
                      HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        MessageBean beanMsg = new MessageBean();
        beanMsg.Subject = "面试审核结果--" + DateUtils.convertToString(DateTime.Now);
        beanMsg.Message = DateUtils.convertToString(DateTime.Now) +":"+this.labName.Text+"应聘"+this.labPosition.Text+ "结果——"+this.radStatus.SelectedItem.Text;
        if (radStatus.SelectedIndex == 2)
        {
            beanMsg.Message = beanMsg.Message + ",录用到岗日期：" + this.txtEmployDate.Text;
        }
        beanMsg.MessageType = 3;
        beanMsg.SendDate = DateTime.Now;
        beanMsg.ReplyFlag = false;
        beanMsg.ReaderHistoryFlag = false;
        beanMsg.ImportantFlag = false;
        beanMsg.EmployeeId = emplyId;
        beanMsg.UserId = loginId;
        CommonSendMessageUtils sendMsg = new CommonSendMessageUtils(beanMsg, Convert.ToString(ViewState["EVALUATER_ID"]));
        sendMsg.SendMessage();   

    }
}
